package com.xuetang9.fujian.socketdemo.tcpdemo.onetoone;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author: fujian
 * @description:
 * @date: 2021/5/10 8:32
 * @version: V1.0
 * @package: com.xuetang9.fujian.socketdemo.tcpdemo.onetoone
 **/
public class Server {
    private static final int port = 9527;

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 创建服务端对象
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("服务端已开启，正在监听端口：" + port);
        // 当前线程进入阻塞状态 BIO
        Socket clientSocket = serverSocket.accept();
        InputStream inputStream = clientSocket.getInputStream();
        OutputStream outputStream = clientSocket.getOutputStream();

       ObjectInputStream inStream = new ObjectInputStream(inputStream);
       ObjectOutputStream outStream = new ObjectOutputStream(outputStream);

       while (true){
           String message = inStream.readObject().toString();
           System.out.println("接收到来自客户端的消息：" + message);
           if ("bye".equals(message)){
               break;
           }
           outStream.writeObject("en~~~~~~~~~~~~~");
           outStream.flush();
       }
        System.out.println("服务端已断开连接");



    }
}
